<?php

/**
 * AccountObligations Controller
 *
 * @property AccountObligation $AccountObligation
 */
class AccountObligationsController extends AppController {

    public $name = "AccountObligations";

    /**
     * index method
     *
     * @return void
     */
    public function index($id) {
        $this->set('id_cuenta', $id);
        $this->AccountObligation->recursive = 0;
        $datos = $this->AccountObligation->find('all', array('conditions' => array('AccountObligation.account_id' => $id), 'fields' => array('AccountObligation.descripcion_acciones', 'Obligation.descripcion', 'AccountObligation.id', 'AccountObligation.orden'), 'order' => array('AccountObligation.orden ASC', 'AccountObligation.fecha_inicio ASC')));
        $this->set('accountObligations', $datos);
    }

    /**
     * view method
     *
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->AccountObligation->id = $id;
        $this->set('accountObligation', $this->AccountObligation->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add($id) {
        $this->layout = "ajax";
        if (!empty($this->data)) {
            $this->AccountObligation->create();
            if ($this->AccountObligation->saveAll($this->data)) {
                $this->Session->setFlash('Registro guardado con éxito', 'flash_custom');
                $this->redirect(array('action' => 'index', $id));
            } else {
                $this->Session->setFlash('Error al guardar el registro. Por favor intentelo nuevamente.');
            }
        }
        $this->data['AccountObligation']['account_id'] = $id;
        $this->AccountObligation->Account->recursive = -1;
        $obligations = $this->AccountObligation->Obligation->recursive = -1;
        $accounts = $this->AccountObligation->Account->find('first', array('conditions' => array('Account.id' => $id), 'fields' => array('Account.contract_id')));
        $obligations = $this->AccountObligation->Obligation->find('list', array('conditions' => array('Obligation.contract_id' => $accounts['Account']['contract_id']), 'fields' => array('Obligation.id', 'Obligation.descripcion')));
        $this->set(compact('obligations', 'accounts'));
    }

    /**
     * edit method
     *
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->layout = "ajax";
        $this->AccountObligation->id = $id;
        $this->AccountObligation->recursive = -1;
        if (!empty($this->data)) {
            if ($this->AccountObligation->saveAll($this->data)) {
                $this->Session->setFlash('Registro editado con éxito', 'flash_custom');
                $this->redirect(array('action' => 'index', $this->data['AccountObligation']['account_id']));
            } else {
                $this->Session->setFlash('No fue posible guardar el registro. Por favor intentelo nuevamente.', 'flash_custom');
            }
        } else {
            $this->data = $this->AccountObligation->read(null, $id);
        }
        $obligations = $this->AccountObligation->Obligation->find('list');
        $accounts = $this->AccountObligation->Account->find('list');
        $this->set(compact('obligations', 'accounts'));
    }

    /**
     * delete method
     *
     * @param string $id
     * @return void
     */
    public function delete($id = null, $account_id = null) {
        $this->layout = "ajax";
        if ($this->AccountObligation->delete()) {
            $this->Session->setFlash('Registro borrado', 'flash_custom');
            $this->redirect(array('action' => 'index', $account_id));
        } else {
            $this->Session->setFlash('No fue posible eliminar el registro.');
            $this->redirect(array('action' => 'index', $account_id));
        }
    }

    public function print_report($cuenta_id) {
        $this->layout = "pdf";
        $this->AccountObligation->recursive = 0;
        $datos = $this->AccountObligation->find('all', array('conditions' => array('AccountObligation.account_id' => $cuenta_id), 'fields' => array('AccountObligation.descripcion_acciones', 'AccountObligation.documentos_soporte', 'Obligation.descripcion', 'AccountObligation.id', 'AccountObligation.estado', 'AccountObligation.soportes_magneticos', 'AccountObligation.fecha_cierre', 'AccountObligation.fecha_inicio'), 'order' => array("AccountObligation.orden ASC", 'AccountObligation.fecha_inicio ASC')));
        $this->set('accountObligations', $datos);
        $cuenta = $this->AccountObligation->Account->find('first', array('conditions' => array('Account.id' => $cuenta_id), 'fields' => array('Account.contract_id', 'Account.fecha_inicio', 'Account.fecha_final')));
        $this->set('cuenta', $cuenta);
        $contrato_id = $cuenta['Account']['contract_id'];
        $options['recursive'] = -1;
        $options['joins'] = array(
            array('table' => 'users',
                'alias' => 'User',
                'type' => 'Left',
                'conditions' => array(
                    'Contract.user_id = User.id'
                )
            ),
            array('table' => 'branches',
                'alias' => 'Branch',
                'type' => 'Left',
                'conditions' => array(
                    'User.branch_id = Branch.id'
                )
            )
        );
        $options['conditions'] = array('Contract.id' => $contrato_id);
        $options['fields'] = array('User.nombre', 'User.primer_apellido', 'User.email', 'User.segundo_apellido', 'Contract.numero', 'User.telefono', 'Contract.dependencia', 'Contract.objeto', 'Contract.interventor', 'Branch.nombre');
        $this->loadModel('Contract');
        $contrato = $this->Contract->find('first', $options);
        $this->set('contrato', $contrato);
    }

    public function total_report($contract_id) {
        $this->layout = "ajax";
        $this->AccountObligation->recursive = 0;
        //$this->response->header(array('Content-type: application/pdf'));
        $datos = $this->AccountObligation->find('all', array('conditions' => array('Account.contract_id' => $contract_id), 'fields' => array('AccountObligation.descripcion_acciones', 'AccountObligation.documentos_soporte', 'AccountObligation.estado', 'AccountObligation.soportes_magneticos', 'Obligation.descripcion', 'AccountObligation.id', 'AccountObligation.fecha_cierre', 'AccountObligation.fecha_inicio'), 'order' => array('AccountObligation.fecha_inicio ASC', 'AccountObligation.orden ASC')));
        $this->set('accountObligations', $datos);
        App::Import('Model', 'Contract');
        $con = new Contract();
        $con->recursive = -1;
        $options['joins'] = array(
            array('table' => 'users',
                'alias' => 'User',
                'type' => 'Left',
                'conditions' => array(
                    'Contract.user_id = User.id'
                )
            ),
            array('table' => 'branches',
                'alias' => 'Branch',
                'type' => 'Left',
                'conditions' => array(
                    'User.branch_id = Branch.id'
                )
            )
        );
        $options['conditions'] = array('Contract.id' => $contract_id);
        $options['fields'] = array('User.nombre', 'User.primer_apellido', 'User.segundo_apellido', 'Contract.numero', 'Contract.fecha_inicio', 'Contract.fecha_final', 'User.telefono', 'Contract.dependencia', 'Contract.objeto', 'Contract.interventor', 'Branch.nombre');
        $contrato = $con->find('first', $options);
        $this->set('contrato', $contrato);
    }

}
